package org.agony2d.gpu.core {
	import flash.geom.Point;
	import org.agony2d.gpu.core.NodeAA
	import org.agony2d.gpu.core.VertexCombine
	import org.agony2d.gpu.core.VertexConvergence
	import org.agony2d.gpu.core.WorldAA;
	import org.agony2d.gpu.supportClasses.textures.TextureProxy
	import org.agony2d.core.agony_internal;
	import org.agony2d.input.Touch;
	
	use namespace agony_internal;
	
public class ImageAABase extends NodeAA {
	
    public function ImageAABase() {
		m_combine = VertexCombine.NewVertexCombine()
    }
  	
	public function get textureData() : String {
		return m_textureData
	}
	
	public function set textureData( v:String ) : void {
		if (m_textureData != v) {
			m_textureData = v
			m_texture = m_fullTexture.getTexture(m_textureData)
			m_combine.setUV(m_texture.left, m_texture.right, m_texture.top, m_texture.bottom)
		}
	}
	
	
	agony_internal var m_combine:VertexCombine
	agony_internal var m_texture:TextureProxy
	agony_internal var m_textureData:String
	agony_internal var m_bindingTouch:Touch
	agony_internal var m_bindingHoverNode:ImageAABase
	
	
	override agony_internal function dispose() : void {
		super.dispose()
		m_combine.recycle()
	}
	
	override agony_internal function render( vertexChanged:Boolean, tinted:Boolean ) : void {
		// changed...
		if (vertexChanged || m_altered) {
			// matrix...
			if (m_altered) {
				this.updateNodeMatrix()
			}
			// vertex
			cachedMatrix.copyFrom(m_matrix)
			cachedMatrix.concat(g_matrixQueue)
			m_combine.setVertexCoords(m_texture.m_realityWidth, m_texture.m_realityHeight, cachedMatrix)
		}
		g_convergence.bytes.writeBytes(m_combine.bytes)
	}
	
	override agony_internal function checkCollisionNode( localX:Number, localY:Number, forInteract:Boolean ) : NodeAA {
		if ((forInteract && m_intouchable) || m_invisible) {
			return null
		}
		if (m_collisionMethod != null) {
			return m_collisionMethod(localX, localY)
		}
		cachedRect.setTo(0, 0, m_texture.m_realityWidth, m_texture.m_realityHeight)
		cachedPoint.setTo(localX, localY)
		return cachedRect.containsPoint(cachedPoint) ? this : null
	}
	
	agony_internal function bubble( type:String, toWorld:Boolean ) : void {
		var node:NodeAA
		
		node = this
		node.dispatchDirectEvent(type)
		node = node.m_parent
		while (node && (toWorld || !(node is WorldAA))) {
			node.dispatchDirectEvent(type)
			node = node.m_parent
		}
	}
}
}